package com.tbit.main.service.impl;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.tbit.main.constant.BatteryConstant;
import com.tbit.main.constant.GatewayProtocol;
import com.tbit.main.constant.RedisConstant;
import com.tbit.main.pojo.Battery;
import com.tbit.main.pojo.Machine;
import com.tbit.main.pojo.TerBattery;
import com.tbit.main.pojo.TerControlMsg;
import com.tbit.main.service.BatteryService;
import com.tbit.main.service.DataService;
import com.tbit.main.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * @author yao
 * @since 2025/2/11 11:06
 */
@Slf4j
@Service
public class TerBatteryListener {

    @Resource
    private BatteryService batteryService;

    @Resource
    private DataService dataService;
    @Resource
    private RedisService redisService;

    @RabbitListener(queues = {"${mq.queue.tbit_uqbike_maintain_ter_battery}"}, concurrency = "5")
    @RabbitHandler
    public void onMessage(Message msg) {
        try {
            String respMsg = new String(msg.getBody(), StandardCharsets.UTF_8);

            TerControlMsg<String> terMsg = JSONUtil.toBean(respMsg, new TypeReference<TerControlMsg<String>>() {
            }, true);

            /* 判断消息类型*/
            if (Objects.equals(terMsg.getMsgId(), GatewayProtocol.MQ_MSG_ID_BATTERY_PUSH)) {
                // 电池数据
                TerBattery terBattery = JSONUtil.toBean(terMsg.getData(), TerBattery.class);

                String batteryNO = terBattery.getBatteryId();
                if (StrUtil.isBlank(batteryNO)) {
                    return;
                }

                Integer machineId = dataService.getMachineIdLister(terBattery.getMachineNO());
                if (machineId == null) {
                    return;
                }

                Machine machine = dataService.getMachine(machineId);
                if (machine == null) {
                    return;
                }
                if (dataService.getBatteryIdNoDb(batteryNO) == null) {
                    //电池不存在直接return
                    return;
                }

                if (StrUtil.isNotBlank(machine.getUserCode())) {
                    Battery batteryOld = batteryService.getByMachineId(machineId);
                    if (batteryOld != null && Objects.equals(batteryNO, batteryOld.getBatteryNO())) {
                        //如果电池前后无变化直接跳过更新
                        return;
                    }
                    //和表里面的不一致才清空
                    if (batteryOld != null && !Objects.equals(batteryNO, batteryOld.getBatteryNO())) {
                        batteryService.updateIdState(batteryNO, machine.getUserCode(), null, BatteryConstant.Battery_State_DRK, null);
                    }
                    // 装载
                    batteryService.updateIdStateCode(batteryNO, machine.getUserCode(), machineId);
                }
            } else {
                log.warn("[TerBatteryListener][未解析的消息类型: {}]", terMsg.getMsgId());
            }

        } catch (Exception e) {
            log.error("[TerBatteryListener] {}", e.getMessage(), e);
        }
    }

}
